Skip to content

AVRO-4324: [Java] Align ReflectDatumReader.readArray with GenericDatumReader eager-allocation guards - #3920

Open
iemejia wants to merge 1 commit into
apache:mainfrom
iemejia:AVRO-4324-reflectdatumreader-readarray
Open

AVRO-4324: [Java] Align ReflectDatumReader.readArray with GenericDatumReader eager-allocation guards#3920
iemejia wants to merge 1 commit into
apache:mainfrom
iemejia:AVRO-4324-reflectdatumreader-readarray

Conversation

@iemejia

@iemejia iemejia commented Aug 5, 2026

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

GenericDatumReader.readArray already validates a declared array block count against the bytes actually remaining (ensureAvailableCollectionBytes) and applies a heap-aware bound for element types whose minimum encoded size is zero, before eagerly allocating the backing storage.

The ReflectDatumReader.readArray override did not apply these guards: it called newArray(old, (int) l, ...)Array.newInstance(elementClass, count) using the declared block count directly, so a malformed or truncated record mapped to a Java array field (for example long[]) could drive a large eager allocation before a single element was read.

This aligns ReflectDatumReader.readArray with the generic reader by applying the same ensureAvailableCollectionBytes and checkMaxCollectionAllocation guards before the allocation. Malformed input now fails fast (EOFException) instead of over-allocating; valid arrays read unchanged.

How was this patch tested?

  • New test in TestReflectDatumReader:
    • read_PojoWithArray_rejectsOversizedArrayCount — a record declaring a ~2e9 array block count with no elements now fails fast instead of attempting a large allocation. Existing round-trip tests act as negative controls.
  • Full avro module test suite passes.

JIRA

ReflectDatumReader.readArray allocated the backing Java array for the declared
array block count (Array.newInstance) before reading any element, unlike
GenericDatumReader.readArray which already validates the count against the bytes
remaining and caps element types whose minimum encoded size is zero.

Apply the same guards (ensureAvailableCollectionBytes plus
checkMaxCollectionAllocation for zero-byte element types) before the eager
allocation, so a malformed or truncated record mapped to a Java array field
(e.g. long[]) fails fast with an EOFException instead of over-allocating. Valid
arrays continue to read unchanged.
@github-actions github-actions Bot added the Java Pull Requests for Java binding label Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java Pull Requests for Java binding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant